package com.easy.controller;

import com.easy.util.ImageUtil;
import com.easy.util.Result;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/upload")
@CrossOrigin
public class UploadController {
    @RequestMapping("/file")
    public Result upload(@RequestParam("file") MultipartFile file){
        System.out.println("ceshi");
        try{
            InputStream is = file.getInputStream();
            String filename = file.getOriginalFilename();
            String newFileName = ImageUtil.upload(is,filename);
            return Result.success(newFileName);
        }catch(IOException e){
            e.printStackTrace();
            throw new RuntimeException("图片上传失败");
        }

    }
}
